<style scoped>
  .block {
    width: 148px;
    height: 148px;
    margin: 0 30px;
  }
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="语言" prop="langueType">
        <el-select v-model="queryParams.langueType" placeholder="语言" clearable size="small" @change="langueTypeChange">
          <el-option
            v-for="dict in langueTypeOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="code">
        <el-select v-model="queryParams.code" placeholder="荣誉类型" clearable size="small">
          <el-option
            v-for="dict in typeLangueOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['/honor/insert']"
        >新增</el-button>
      </el-col>
    </el-row>

    <div style="width:100%;" v-if="honorList">
      <div style="display:inline-block;"
        v-for="honor in honorList"
        :key="honor.id"
      >
        <div  class="block">
          <img style="width:100%;height:100%;" class="el-upload-list__item-thumbnail"  alt="" :src="honor.img">
        </div>
        <div style="text-align:center;">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(honor)"
            v-hasPermi="['/honor/updateById']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(honor)"
            v-hasPermi="['/honor/deleteById']"
          >删除</el-button>
        </div>
      </div>
    </div>

    <!-- 添加或修改公司荣誉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件" prop="img">
              <uploadImage v-model="form.img"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型" prop="code">
              <el-select v-model="langueTypeUpdate" placeholder="语言" clearable size="small" @change="langueTypeUpdateChange">
                <el-option
                  v-for="dict in langueTypeOptions"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.val"
                />
              </el-select>
              <el-select v-model="form.code" placeholder="请选择">
                <el-option
                  v-for="dict in typeLangueUpdateOptions"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="顺序" prop="sortNum">
              <el-input v-model="form.sortNum" placeholder="请输入顺序" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHonor, getHonor, delHonor, addHonor, updateHonor } from "@/api/honorFirmCert/honor/info";
import { listHonorType } from "@/api/honorFirmCert/honor/type";
import UploadImage from '@/components/UploadImage';
import Editor from '@/components/Editor';
import { SORT_NUM } from "@/constant/constant";
import { LANGUE_TYPE, FILE_TYPE } from "@/enum/enum";

export default {
  name: "Honor",
  components: {
    UploadImage,
    Editor
  },
  data() {
    return {
      // 枚举属性
      // 文件类型
      eFileType: FILE_TYPE,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司荣誉表格数据
      honorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 类型数据字典
      typeLangueOptions: [],
      // 语言类型数据字典
      langueTypeOptions: [],
      // 类型数据字典
      typeLangueUpdateOptions: [],
      // 添加修改时候的语言类型
      langueTypeUpdate: undefined,
      // 查询参数
      queryParams: {
        langueType: LANGUE_TYPE,
        code: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        img: [
          { required: true, message: "文件不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
      this.getDicts("langue_type").then(response => {
        response.data.forEach(element => {
          element.val = parseInt(element.val)
        });
        this.langueTypeOptions = response.data;
      });
      listHonorType().then(response => {
        this.typeOptions = response.data;
        this.langueTypeChange();
      });
  },
  methods: {
    /** 查询公司荣誉列表 */
    getList() {
      this.loading = true;
      listHonor(this.queryParams).then(response => {
        this.honorList = response.data;
        this.honorList.forEach(element => {
          element.img = process.env.VUE_APP_STATIC_FILE_URL + element.img;
        });
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 语言改变的时候触发
    langueTypeChange() {
      if(this.queryParams.langueType){
        this.typeLangueOptions = [];
        this.typeOptions.forEach(element => {
          if(element.langueType == this.queryParams.langueType){
            this.typeLangueOptions.push(element);
          }
        });
      }else{
        this.typeLangueOptions = this.typeOptions;
      }
      if(this.queryParams.code){
        if(this.typeLangueOptions.length > 0){
          this.queryParams.code = this.typeLangueOptions[0].code
        }else{
          this.queryParams.code = undefined
        }
      }
    },
    // 语言改变的时候触发
    langueTypeUpdateChange() {
      if(this.langueTypeUpdate){
        this.typeLangueUpdateOptions = [];
        this.typeOptions.forEach(element => {
          if(element.langueType == this.langueTypeUpdate){
            this.typeLangueUpdateOptions.push(element);
          }
        });
      }else{
        this.typeLangueUpdateOptions = this.typeOptions;
      }
      if(this.form.code){
        if(this.typeLangueUpdateOptions.length > 0){
          this.form.code = this.typeLangueUpdateOptions[0].code
        }else{
          this.form.code = undefined
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        img: undefined,
        code: this.queryParams.code,
        sortNum: SORT_NUM
      };
      this.langueTypeUpdate = this.queryParams.langueType;
      this.typeLangueUpdateOptions = this.typeLangueOptions;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.langueTypeChange();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司荣誉";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHonor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司荣誉";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateHonor(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHonor(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除公司荣誉编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHonor(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
  }
};
</script>